import pandas as pd

def process_csv(input_file, output_file):
    # 读取CSV文件
    df = pd.read_csv(input_file)

    # 去掉路径中的前缀
    df['Path'] = df['image_path'].str.replace('/home/challenge/dataset/testingdata/test_set_A_rename/', '')

    # 转换x1, y1, x2, y2为Polygon格式
    df['Polygon'] = df.apply(lambda row: [[[row['x1'], row['y1']], [row['x2'], row['y1']], 
                                           [row['x2'], row['y2']], [row['x1'], row['y2']]]], axis=1)

    # 选择需要的列
    df_final = df[['Path', 'Polygon', 'score']]

    # 保存到新的CSV文件
    df_final.to_csv(output_file, index=False)
    print(f"文件已保存到: {output_file}")

# 调用函数处理CSV文件
input_file = '/home/challenge/dataset/mmdet_bisai/inferres/detection_results202410161642.csv'  # 输入文件路径
output_file = 'output.csv'  # 输出文件路径
process_csv(input_file, output_file)
